
use "$raw/s6_background_noPII.dta", clear
*******cleaning******

*An indicator for whether one or both parents are dead
gen parents="Both Alive" if stateofbothparentsbothaliv=="Both Alive" | stateofbothparentsbothaliv=="Both alive" | stateofbothparentsbothaliv=="both alive"  | stateofbothparentsbothaliv=="both living"
replace parents="Mother alive" if stateofbothparentsbothaliv=="Mother alive" | stateofbothparentsbothaliv=="Mother living" |stateofbothparentsbothaliv=="mother living" 
replace parents="Father alive" if stateofbothparentsbothaliv=="Father alive" | stateofbothparentsbothaliv=="Father living" |stateofbothparentsbothaliv=="father living" 
replace parents="Both dead" if stateofbothparentsbothaliv=="Both Dead" | stateofbothparentsbothaliv=="Both dead" |stateofbothparentsbothaliv=="both dead"  
gen orphan=0
replace orphan=1 if parents!="Both Alive"
*replace orphan=1 if parents=="Both dead" | parents=="Mother alive"
la var orphan "Orphan"
gen mother_raised=0
replace mother_raised=1 if parents=="Mother alive"

*1. An indicator for whether the student comes from Makindye district (where Katwe is located)
*2. An indicator for whether the student comes from Kampala
replace parish=lower(parish)
replace subcounty=lower(subcounty)
replace district=lower(district)

gen kampala=0
replace kampala=1 if  parish=="kampala" | subcounty=="kampala" | district=="kampala"
la var kampala "From Kampala"
tab kampala
//19% from kampala

*An indicator for whether the student performed below the median in the sample in their previous national exams (PLE for S4 students and UCE for S6 students).
tab UCEresult
_pctile UCEresult if UCEresult!=.
return list
gen below_median_UCE=0 if UCEresult!=.
replace below_median_UCE=1 if UCEresult>=r(r1) & UCEresult!=. // higher scores are worse
la var below_median_UCE "Below median UCE"

tab PLEresult
_pctile PLEresult if PLEresult!=.
return list 
gen below_median_PLE=0 if PLEresult!=.
replace below_median_PLE=1 if PLEresult>r(r1) & PLEresult!=. // higher scores are worse
la var below_median_PLE "Below median PLE"

gen below_median_school=0 if (PLEresult!=. | UCEresult!=.)
replace below_median_school=1 if below_median_UCE==1 
replace below_median_school=1 if below_median_PLE==1 & UCEresult==.
la var below_median_school "Below median prior school"


*An indicator for whether either parent had a professional occupation (teacher, accountant, lawyer, doctor, engineer, civil servant)
replace fathersoccupation=lower(fathersoccupation )
replace mothersoccupation=lower(mothersoccupation )

local professional "accountant architect auditor building engineer "civil engineer" "civil servant" "councillor lc 3" "district planner" doctor "electrical engineer" engineer lawyer "lc2 chairman" lecturer mayor "principal researcher" teacher  "technical engineer" coucellor banker "head mistress" journalist researcher "school burser""
gen profession_dad=0
gen profession=0
gen profession_mum=0
foreach x of local professional {
replace profession=1 if mothersoccupation=="`x'" | fathersoccupation=="`x'"
replace profession_dad=1 if fathersoccupation=="`x'"
replace profession_mum=1 if mothersoccupation=="`x'" 

}
la var profession "One or both parents professionals"


*. An indicator for whether financial support was being provided to the student
gen bursary=0
replace bursary=1 if bursarygranted=="yes"
la var bursary "Received a bursary"

*save
save "$cleaned/s6_background_cleaned.dta", replace



////combine with S6 data
use "$cleaned/all_s6_cleaned.dta", clear
merge 1:1 id using "$cleaned/s6_background_cleaned.dta"
drop if _merge==1

gen std_principal2_mock_miss=0
replace std_principal2_mock_miss=1 if std_principal2_mock==.
replace std_principal2_mock=POINTS2_mock if std_principal2_mock==.
gen std_subsidiary2_mock_miss=0
replace std_subsidiary2_mock_miss=1 if std_subsidiary2_mock==.
replace std_subsidiary2_mock=POINTS2_mock if std_subsidiary2_mock==.

**************************************
*	table A2 
**************************************

global subgroup below_median_school kampala  mother_raised profession bursary
*local subgroup  profession 
foreach x of global subgroup { 

sum `x' if treatment==0
sum `x' if treatment==1
capture gen treatment_`x'=treatment*`x'
local label: var la `x'
label variable treatment_`x' "Treatment * `label'"

}

eststo placebo:  estpost summarize ///
    $subgroup if treatment == 0
eststo treatment:  estpost summarize ///
    $subgroup if treatment == 1
eststo diff:  estpost ttest ///
   $subgroup, by(treatment) unequal
   esttab  placebo treatment diff using "$output/table_A2.tex", ///
cells("mean(pattern(1 1 0) fmt(2)) sd(pattern(1 1 0)) b(star pattern(0 0 1) fmt(2))  p(pattern(0 0 1) par fmt(2))") ///
label replace

**************************************
*	table A13
**************************************
preserve 
foreach y of global subgroup  {
local i=1
tempfile tf`y'`i' 
reghdfe std_POINTS2 POINTS2_mock treatment  treatment_`y' `y' school6 school7 school8 school10 school11 $controls , vce(robust) $reghdfe 
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`y'`i' 
reghdfe std_principal2 std_principal2_mock std_principal2_mock_miss treatment treatment_`y' school6 school7 school8 school10 school11  $controls `y', vce(robust)  $reghdfe
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')
local i=`i'+1
tempfile tf`y'`i' 
reghdfe std_subsidiary2  std_subsidiary2_mock std_subsidiary2_mock_miss treatment  treatment_`y'  school6 school7 school8 school10 school11  $controls `y', vce(robust)  $reghdfe
parmest, format(p %12.9f) saving(`tf`y'`i'',replace) idn(`i')
}

local j=1
foreach y of global subgroup  {
dsconcat `tf`y'1' `tf`y'2' `tf`y'3' 
keep if parm=="treatment_`y'"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
replace q=round(q, 0.01)
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/3 {
global Tbq`j'`x'=q[`x']
display ${Tbq`j'`x'}
}

dsconcat `tf`y'1' `tf`y'2' `tf`y'3' 
keep if parm=="treatment"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
replace q=0.99 if q==1
replace q=round(q, 0.01)
*qqvalue p, method(bonferroni) qvalue(q2)
foreach x of numlist 1/3 {
global Tq`j'`x'=q[`x']
display ${Tq`j'`x'}
}
local j=`j'+1
}
restore




local i=1 
foreach x of global subgroup { 
reghdfe std_POINTS2 POINTS2_mock treatment  treatment_`x' `x' school6 school7 school8 school10 school11 $controls , vce(robust) $reghdfe 
sum std_POINTS2 if treatment==0 & `x'==1
local mean=r(mean)
sum std_POINTS2 if treatment==0 & `x'==0
local mean2=r(mean)
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value=r(p)
if `i'==1 {
outreg2 using "${output}/table_A13.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_POINTS2 treatment treatment_`x' `x' ${controls} POINTS2_mock) addstat(mean, `mean', mean2, `mean2', treated, `F1c', SE , `F1se', p_value, `p_value', qvalue, ${Tq`i'1}, qvalue interaction,${Tbq`i'1}) replace label adec(2)
}
else {
outreg2 using "${output}/table_A13.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_POINTS2 treatment treatment_`x' `x' ${controls} POINTS2_mock) addstat(mean, `mean', mean2, `mean2',treated, `F1c', SE , `F1se', p_value, `p_value', qvalue, ${Tq`i'1}, qvalue interaction,${Tbq`i'1}) label adec(2)
}
reghdfe std_principal2 std_principal2_mock std_principal2_mock_miss treatment treatment_`x' school6 school7 school8 school10 school11  $controls `x', vce(robust)  $reghdfe
sum std_principal2 if treatment==0 & `x'==1
local mean=r(mean)
sum std_principal2 if treatment==0 & `x'==0
local mean2=r(mean)
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value=r(p)
outreg2 using "${output}/table_A13.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_principal2 treatment treatment_`x' `x'  ${controls} std_principal2_mock) addstat(mean, `mean', mean2, `mean2', treated, `F1c', SE , `F1se', p_value, `p_value', qvalue, ${Tq`i'2}, qvalue interaction,${Tbq`i'2})   label adec(2)

reghdfe std_subsidiary2  std_subsidiary2_mock std_subsidiary2_mock_miss treatment  treatment_`x'  school6 school7 school8 school10 school11  $controls `x', vce(robust)  $reghdfe
sum std_subsidiary2 if treatment==0 & `x'==1
local mean=r(mean)
sum std_subsidiary2 if treatment==0 & `x'==0
local mean2=r(mean)
lincom treatment_`x' + treatment
local F1c = r(estimate)
local F1se = r(se)
test `x' + treatment_`x' + treatment = `x'
local p_value = r(p)
outreg2 using "${output}/table_A13.tex", dec(2) title(Impact of treatment assignment on standardized test scores) keep(std_subsidiary2 treatment treatment_`x' `x'  ${controls} std_subsidiary2_mock) addstat(mean, `mean', mean2, `mean2', treated, `F1c',SE , `F1se', p_value, `p_value', qvalue, ${Tq`i'3}, qvalue interaction,${Tbq`i'3}) label adec(2)

local i=`i'+1
}

